<?php
include './config/config.php';

$where = 1;

$content = $_GET['content'];

if (!empty($content)) {
  $where = "`j`.`name` LIKE '%$content%' OR `d`.`name` LIKE '%$content%'";
}

// 获取当前的页码
$page = isset($_GET['page']) ? $_GET['page'] : 1;

// 获取person表的数据总数
$CountSql = "SELECT COUNT(*) AS num FROM `pre_job` AS j LEFT JOIN `pre_department` AS d ON j.depid = d.id WHERE $where";
$count = query($CountSql);

// 每页显示多少条数据
$limit = 5;

// 每页显示多少个分页的页码数
$size = 5;

$ShowPage = page($page, $count['num'], $limit, $size);

// 偏移量 -> 索引值
// ? $start = ($page - 1); 有时候偏移量需要 乘上 $limit
$start = ($page - 1) * $limit;


$querySql = "SELECT j.*,d.name as dname FROM `pre_job` AS j LEFT JOIN `pre_department` AS d ON j.depid = d.id WHERE $where ORDER BY j.id ASC LIMIT $start,$limit";

$dataList = queryAll($querySql);

if ($_POST) {

  $where = "`id` = '{$_POST['id']}'";

  deleteById("job", $where);
}
?>

<!DOCTYPE html>
<html lang="en">

<head>
  <?php include_once('common/meta.php'); ?>
</head>
<style>
  .pagination {
    text-align: center;
  }

  .btn-toolbar {
    display: flex;
    align-items: center;
  }

  #search_form {
    display: flex;
    align-items: center;
    margin: 0;
    margin-left: 20px;
  }
</style>

<body>
  <!--<![endif]-->

  <?php include_once('common/header.php'); ?>

  <?php include_once('common/menu.php'); ?>

  <div class="content">
    <div class="header">
      <h1 class="page-title">岗位列表</h1>
    </div>
    <ul class="breadcrumb">
      <li>jobList <span class="divider">/</span></li>
    </ul>

    <div class="container-fluid">
      <div class="row-fluid">
        <div class="btn-toolbar">
          <button class="btn btn-primary" onClick="location='jobAdd.php'"><i class="icon-plus"></i>添加岗位</button>

          <?php include_once 'common/form.php'; ?>
        </div>
        <div class="well">
          <table class="table table-hover">
            <thead>
              <tr>
                <th>ID</th>
                <th>岗位名称</th>
                <th>所属部门</th>
                <th style="width: 30px;">操作</th>
              </tr>
            </thead>
            <tbody>
              <?php foreach ($dataList as $item) { ?>
                <tr>
                  <td>
                    <?php echo $item['id']; ?>
                  </td>
                  <td>
                    <?php echo $item['name']; ?>
                  </td>
                  <td>
                    <?php echo $item['dname'] ? $item['dname'] : '未知'; ?>
                  </td>
                  <td>
                    <?php echo "<a href='jobUpdata.php?jobId={$item['id']}'><i class='icon-pencil'></i></a>" ?>

                    <?php echo "<a href='#myModal'  role='button' data-id='{$item['id']}' data-toggle='modal'  class='sureBtn'><i class='icon-remove'></i></a>" ?>
                  </td>
                </tr>
              <?php } ?>
            </tbody>
          </table>
        </div>
        <div class="pagination">
          <?php echo $ShowPage; ?>
        </div>

        <div class="modal small hide fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
          <div class="modal-header">
            <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
            <h3 id="myModalLabel">警告</h3>
          </div>
          <div class="modal-body">
            <p class="error-text"><i class="icon-warning-sign modal-icon"></i>确定删除？</p>
          </div>
          <div class="modal-footer">
            <button class="btn" data-dismiss="modal" aria-hidden="true">取消</button>
            <button class="btn btn-danger deleteBtn" data-dismiss="modal">删除</button>
          </div>
        </div>

        <?php include_once('common/footer.php'); ?>

      </div>
    </div>
  </div>


  <script src="./assets/lib/jquery-1.8.1.min.js"></script>
  <script>
    $('.sureBtn').on('click', function() {
      $('.deleteBtn').attr("data-id", $(this).attr('data-id'))
    });

    $('.deleteBtn').on('click', function() {
      // console.log($(this).attr('data-id'));
      var jobId = $(this).attr('data-id');
      console.log(jobId);
      $.ajax({
        type: "post",
        url: "jobList.php",
        data: {
          id: jobId
        },
        dataType: "json",
        success: function(res) {
          alert('删除成功！');
          location.reload();
        },
        error: function(error) { // 错误回调的函数
          console.log(error);
        }
      });
    })
  </script>
</body>

</html>
<?php include_once('common/script.php'); ?>